<template>
  <div class="app-container">
    <el-card>
      <el-row type="flex" justify="space-between">
        <el-col>  <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="标签名称">
            <el-input v-model="formInline.tagName" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="formInline.state" placeholder="请选择">
              <el-option label="开启" :value="1" />
              <el-option label="屏蔽" :value="0" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click="clear">清空</el-button>
            <el-button type="primary" @click="search">搜索</el-button>
          </el-form-item>
        </el-form>
        </el-col>
        <el-col>
          <el-row type="flex" justify="end">
            <el-link
              v-if="$route.params.fromPath==='subject'"
              href="#/subject"
              icon="el-icon-arrow-left"
              style="margin-right:10px"
            >返回学科</el-link>
            <el-button icon="el-icon-edit" type="success" @click="showDialog=true">新增标签
            </el-button>
          </el-row>
        </el-col>

      </el-row>

      <el-alert
        :title="'数据一共 ' + page.total + ' 条'"
        type="info"
        show-icon
        :closable="false"
      />
      <el-table :data="list" style="width: 100%" stripe>
        <el-table-column type="index" label="序号" width="180" />
        <el-table-column prop="subjectName" label="所属学科" width="180" />
        <el-table-column prop="tagName" label="标签名称" />
        <el-table-column prop="username" label="创建者" width="180" />
        <el-table-column label="创建日期" width="180">
          <!-- 作用域插槽 -->
          <!-- 处理时间格式 -->
          <template v-slot="scope">
            <!-- row:{timeOfEntry} -->
            {{ scope.row.addDate | formatDate }}
          </template>
        </el-table-column>
        <el-table-column :formatter="formatState" prop="state" label="状态" />
        <el-table-column prop="address" label="操作">
          <template v-slot="scope">
            <el-button type="text" @click="editstop(scope.row)">{{ scope.row.state==0?'启用':'屏蔽' }}</el-button>

            <el-button :disabled="scope.row.state==0" type="text" @click="edittag(scope.row)">修改</el-button>
            <el-button :disabled="scope.row.state==0" type="text" @click="deltag(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <el-row type="flex" justify="end" align="middle">
        <el-pagination
          :current-page="page.page"
          :page-sizes="[3,5,7,9]"
          :page-size="page.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-row>
    </el-card>
    <!-- 新增标签弹层组件 -->
    <addtag :show-dialog.sync="showDialog" @addtags="addfn" />
    <edittag ref="child" :show-edit-dialog.sync="showEditDialog " @edittags="editfn" />
  </div>
</template>

<script>
import { gettaglist, deltag, edittagstate } from '@/api/subject'
import addtag from './addtag.vue'
import edittag from './edittag.vue'
import statanum from '@/api/constant/state'
export default {

  components: {
    addtag,
    edittag
  },
  data() {
    return {
      showDialog: false,
      showEditDialog: false,

      // 新增和编辑的下拉菜单数据
      catalist2: [],
      // 标签列表数据
      list: [],
      page: {
        page: 1, // 当前页码
        pagesize: 5, // 每页显示的条数
        total: 0// 服务器的总数据
      },
      formInline: {
        tagName: '',
        state: ''
      }

    }
  },
  created() {
    this.gettaglist(this.page)
  },
  methods: {
    // 子组件发射过来的函数
    addfn() {
      this.gettaglist(this.page)
    },
    editfn() {
      this.gettaglist(this.page)
    },
    // 点击禁用或者开启按钮
    async editstop(data) {
      if (data.state === 1) {
        // data.state = 0
        await edittagstate({
          id: data.id,
          state: 0

        })
        // 重新渲染
        await this.gettaglist(this.page)
        this.$message.success('操作成功')
      } else {
        // data.state = 1
        await edittagstate({
          id: data.id,
          state: 1
        })
        //  重新渲染
        this.gettaglist(this.page)
        this.$message.success('操作成功')
      }
      console.log(data, '当前选中行的标签列表数据')
    },
    // 分页组件
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.page.pagesize = val
      this.gettaglist(this.page)
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.page.page = val
      this.gettaglist(this.page)
    },
    // 点击清空，清空表单，重新渲染列表
    clear() {
      this.formInline = ''
      this.gettaglist(this.page)
    },
    // 点击搜索按钮，完成--搜索功能--
    search() {
      console.log(this.formInline, '搜索框输入的值')
      this.gettaglist(this.formInline)
      console.log(this.list, '根据搜索框筛选的标签列表数据')
    },
    // 调用接口--获取标签列表--数据
    async gettaglist(data) {
      const res = await gettaglist(data)
      console.log(res, '-------------------------')
      this.list = res.items
      console.log(this.list, '标签列表数据')
      this.page.total = res.counts
      console.log(this.page)
    },
    // 调用接口--删除-标签
    async deltag(id) {
      try {
        await this.$confirm('确定删除标签吗')
        await deltag(id)

        this.gettaglist(this.page)

        this.$message.success('删除成功')
      } catch (err) {
        console.log(err)
      }
    },
    // 格式化状态
    formatState(row, column, cellValue, index) {
      // console.log(cellValue)
      // 要去找 1所对应的值
      const obj = statanum.StateType.find((item) => {
        return item.id === cellValue
      })
      return obj ? obj.value : '未知'
    },
    // 点击编辑按获取所属学科信息
    async edittag(data) {
      this.showEditDialog = true
      console.log(data, '-=-=-=-=-')
      this.ruleForm2 = data
      this.$refs.child.ruleForm = this.ruleForm2
    }

  }
}
</script>

<style lang="scss" scoped>

</style>
